<?php

namespace app\api\controller;

use app\admin\utils\ontimeLog;
use app\api\model\CartModel;
use app\api\utils\jsonResult;
use think\Controller;
use think\Log;
use think\Session;

class Cart extends Controller
{
    public function addCart(): \think\response\Json
    {
        $cart = new CartModel();
        if (Session::get('user_id')) {
            $user_id = Session::get('user_id');
//            $user_id = input('param.user_id');
            $pro_no = input('param.goods_id');
            $goods_skuId = input('param.goods_skuId');
            $goods_num = 1;
            $goods_title = $cart->getNameById($pro_no);
            $goods_img = $cart->getImgById($pro_no);
            if ($cart->isExistence($user_id, $goods_title, $goods_skuId)) {
                if ($cart->addNum($goods_title, $goods_num, $user_id, $goods_skuId)) {
                    $count = $cart->getCount($user_id);
                    return (new jsonResult)::result(200, "添加购物车成功", $count);
                } else return (new jsonResult)::result(500, "添加购物车失败");
            } else {

                $skuData = $cart->getSkuById($goods_skuId);
                $data = [
                    'user_id' => $user_id,
                    'goods_title' => $goods_title,
                    'sku_id' => $goods_skuId,
                    'sku_title' => $skuData[0]['sku_title'],
                    'goods_price' => $skuData[0]['sku_price'],
                    'goods_totalPrice' => $goods_num * $skuData[0]['sku_price'],
                    'goods_num' => $goods_num,
                    'img' => $goods_img,

                ];
                $cartData = $cart->add($data);
                if ($cartData) {
                    $count = $cart->getCount($user_id);
                    return (new jsonResult)::result(200, "添加购物车成功", $count);
                } else return (new jsonResult)::result(500, "添加购物车失败");
            }

        } else return (new jsonResult)::result(401, "用户未登陆");

    }

    public function updateCart(): \think\response\Json
    {
//        if (Session::get('user_id')) {
            $cart = new CartModel();
            $goods_num = input('param.goods_num');
            $id = input('param.id');
            if ($goods_num >= 0) {
                if ($cart->update($id, $goods_num)) {
                    return (new jsonResult)::result(200, "更新购物车成功");
                } else return (new jsonResult)::result(500, "更新购物车失败");
            } else $del = $cart->delete($id);
            if ($del) {
                return (new jsonResult)::result(200, "更新购物车成功");
            } else return (new jsonResult)::result(500, "更新购物车失败");
//        } else return (new jsonResult)::result(401, "用户未登陆");


    }

    public function getCart(): \think\response\Json
    {
        if (Session::get('user_id')) {
            $user_id = Session::get('user_id');
//            $user_id = input('param.user_id');
            $cart = new CartModel();

            $cartData = $cart->getByUserId($user_id);
            if ($cartData) {
                return (new jsonResult)::result(200, "获取购物车信息成功", $cartData);
            } else return (new jsonResult)::result(200, "购物车为空");
        } else return (new jsonResult)::result(401, "请先登录");
    }

    public function deleteCart(): \think\response\Json
    {
        if (Session::get('user_id')) {
            $cart = new CartModel();
            $id = input('id');
            $del = $cart->delete($id);
            if ($del) {
                return (new jsonResult)::result(200, "删除成功");
            } else return (new jsonResult)::result(500, "删除失败");
        } else return (new jsonResult)::result(401, "请先登录");
    }


}